describe("api_transfer in India", function() {
	var callback;

	beforeEach(function() {
		App.initSession();
		callback = jasmine.createSpy();
	});

	describe("App.ThirdPartyPayeeListFetcher", function() {
		var fetcher;
        var transferObject;
        beforeEach(function() {
            fetcher = App.ThirdPartyPayeeListFetcher.create();
            transferObject = App.ThirdPartyTransfer.create();
        });

        describe("processList", function() {
        	it("general expect handling", function() {
        		spyOn(bsoi, 'isGeneralException').andReturn(true);
        		spyOn(fetcher, 'callbackTransfer');

        		fetcher.processList(transferObject, {});

        		expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
        	});

        	it("error response handling", function() {
        		spyOn(bsoi, 'isGeneralException').andReturn(false);
        		spyOn(bsoi, 'isSuccessResponse').andReturn(false);
        		spyOn(fetcher, 'callbackTransfer');

        		fetcher.processList(transferObject, {thirdPartyPayeeListResponse:{responseHeader:{errorCode:'', errorMessage:''}}});

        		expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
        	});

            it("response with no payee", function() {
            	spyOn(bsoi, 'isGeneralException').andReturn(false);
        		spyOn(bsoi, 'isSuccessResponse').andReturn(true);
        		spyOn(fetcher, 'callbackTransfer');
        		spyOn(App.i18n.getTranslations(), 'get');

        		fetcher.processList(transferObject, {
        			thirdPartyPayeeListResponse: {
        				payeeList: []
        			}
        		});

        		expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
  				expect(App.i18n.getTranslations().get).toHaveBeenCalledWith('error.noPayeeListError');
            });

            describe("response with payeeList", function() {
            	it("payee type is not 1 or 2 in payeeList", function() {
            		spyOn(bsoi, 'isGeneralException').andReturn(false);
        			spyOn(bsoi, 'isSuccessResponse').andReturn(true);
        			spyOn(fetcher, 'callbackTransfer');
        			spyOn(App.i18n.getTranslations(), 'get');

        			fetcher.processList(transferObject, {
        				thirdPartyPayeeListResponse: {
        					payeeList: [
        						{
									"payeeType": "4"
        						},
        						{
									"payeeType": "4"
        						}
        					],
        					descriptionList: [
        						{
        							"code": "6",
        							"value": "Other"
        						}
        					]
        				}
        			});

				expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
  				expect(App.i18n.getTranslations().get).toHaveBeenCalledWith('error.noPayeeListError');
            	});

            	it("payee type is 1 or 2 in payeeList", function() {
            		spyOn(bsoi, 'isGeneralException').andReturn(false);
        			spyOn(bsoi, 'isSuccessResponse').andReturn(true);
        			var payee = App.ThirdPartyPayee.create({"payeeId": 1, "payeeType": '1'});
        			spyOn(payee, 'setProperties');
        			spyOn(fetcher, 'callbackTransfer');

        			fetcher.processList(transferObject, {
        				thirdPartyPayeeListResponse: {
        					payeeList: [
        						{
        							"payeeId": 1,
									"payeeType": "1"
        						}
        					],
        					descriptionList: [
        						{
        							"code": "6",
        							"value": "Other"
        						}
        					]
        				}
        			});

        			expect(fetcher.callbackTransfer).toHaveBeenCalled();

            	});

            });
        });
	});

	describe("fetchToListForTransfer", function() {
		var listFetcher;
		beforeEach(function() {
			session = App.getSession();
		});

		it("transfer is BillPayment", function() {
			var transfer = {};
			listFetcher = App.BillPayeeListFetcher.create();
			spyOn(listFetcher, 'fetch');

			listFetcher.fetch(transfer, callback);

			expect(listFetcher.fetch).toHaveBeenCalledWith(transfer, callback);
		});
	});

	describe("App.VisaMoneyPayeeListFetcher", function() {
		var fetcher;
        var transferObject;
        beforeEach(function() {
            fetcher = App.VisaMoneyPayeeListFetcher.create();
            transferObject = App.VisaMoneyTransfer.create();
        });

        describe("processList", function() {
        	it("general expect handling", function() {
        		spyOn(bsoi, 'isGeneralException').andReturn(true);
        		spyOn(fetcher, 'callbackTransfer');

        		fetcher.processList(transferObject, {});

        		expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
        	});

        	it("error response handling", function() {
        		spyOn(bsoi, 'isGeneralException').andReturn(false);
        		spyOn(bsoi, 'isSuccessResponse').andReturn(false);
        		spyOn(fetcher, 'callbackTransfer');

        		fetcher.processList(transferObject, {thirdPartyPayeeListResponse:{responseHeader:{errorCode:'', errorMessage:''}}});

        		expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
        	});

            it("response with no payee", function() {
            	spyOn(bsoi, 'isGeneralException').andReturn(false);
        		spyOn(bsoi, 'isSuccessResponse').andReturn(true);
        		spyOn(fetcher, 'callbackTransfer');
        		spyOn(App.i18n.getTranslations(), 'get');

        		fetcher.processList(transferObject, {
        			thirdPartyPayeeListResponse: {
        				payeeList: []
        			}
        		});

        		expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
  				expect(App.i18n.getTranslations().get).toHaveBeenCalledWith('error.noPayeeListError');
            });

            describe("response with payeeList", function() {
            	it("payee type is not 4 in payeeList", function() {
            		spyOn(bsoi, 'isGeneralException').andReturn(false);
        			spyOn(bsoi, 'isSuccessResponse').andReturn(true);
        			spyOn(fetcher, 'callbackTransfer');
        			spyOn(App.i18n.getTranslations(), 'get');

        			fetcher.processList(transferObject, {
        				thirdPartyPayeeListResponse: {
        					payeeList: [
        						{
									"payeeType": "1"
        						},
        						{
									"payeeType": "2"
        						}
        					],
        					descriptionList: [
        						{
        							"code": "6",
        							"value": "Other"
        						}
        					]
        				}
        			});

					expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
  					expect(App.i18n.getTranslations().get).toHaveBeenCalledWith('error.noPayeeListError');
            	});

            	it("payee type is 4 in payeeList", function() {
            		spyOn(bsoi, 'isGeneralException').andReturn(false);
        			spyOn(bsoi, 'isSuccessResponse').andReturn(true);
        			var payee = App.ThirdPartyPayee.create({"payeeId": 1, "payeeType": '4'});
        			spyOn(payee, 'setProperties');
        			spyOn(fetcher, 'callbackTransfer');

        			fetcher.processList(transferObject, {
        				thirdPartyPayeeListResponse: {
        					payeeList: [
        						{
        							"payeeId": 1,
									"payeeType": "4"
        						}
        					],
        					descriptionList: [
        						{
        							"code": "6",
        							"value": "Other"
        						}
        					]
        				}
        			});

        			expect(fetcher.callbackTransfer).toHaveBeenCalled();

            	});

            });
        });
	});
    describe("App.ViewAndPayBillPayeeListFetcher", function() {
        var fetcher;
        var transferObject;
        beforeEach(function() {
            fetcher = App.ViewAndPayBillPayeeListFetcher.create();
            transferObject = App.ViewAndPayBillPayment.create();
        });

        describe("fetch", function() {
            it("error response handling", function() {

                spyOn(fetcher, 'processList');
                spyOn(bsoi, 'getviewAndPayBillerList');

                fetcher.fetch(transferObject, callback);

                expect(fetcher.callbackTransfer).toEqual(callback);
                expect(bsoi.getviewAndPayBillerList).toHaveBeenCalled();

                var args = bsoi.getviewAndPayBillerList.mostRecentCall.args;
                var requestData = {
                    biller:{
                        countryCode:'IN'
                    },
                    requestHeader : session.getBreezeContext()
                };
                var response = jasmine.createSpy();

                expect(args[0]).toEqual(requestData);

                args[1](response);
                expect(fetcher.processList).toHaveBeenCalledWith(transferObject, response);
            });
        });

        describe("processList", function() {
            it("error response handling", function() {
                spyOn(bsoi, 'validateResponse').andThrow(BreezeException.create());
                spyOn(fetcher, 'callbackTransfer');

                fetcher.processList(transferObject, {billerListResponse:{responseHeader:{errorCode:'', errorMessage:''}}});

                expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
            });

            it("response with no payee", function() {
                spyOn(bsoi, 'validateResponse');
                spyOn(fetcher, 'callbackTransfer');
                spyOn(App.i18n.getTranslations(), 'get');

                fetcher.processList(transferObject, {
                    billerListResponse: {
                        billers: []
                    }
                });

                expect(fetcher.callbackTransfer).toHaveBeenCalledWith(null, jasmine.any(BreezeException));
                expect(App.i18n.getTranslations().get).toHaveBeenCalledWith('error.noPayeeListError');
            });

            describe("response with payeeList", function() {
                it("response with billers", function() {
                    spyOn(bsoi, 'validateResponse');
                    spyOn(fetcher, 'callbackTransfer');
                    fetcher.processList(transferObject, {
                        billerListResponse: {
                            billers: [
                                {
                                    "payee": {
                                    }
                                },
                                {
                                    "payee": {
                                    }
                                }
                            ]
                        }
                    });

                    expect(fetcher.callbackTransfer).toHaveBeenCalled()
                });
            });
        });
    });

	describe("App.FromListFetcher -- fetch", function(){
		var fromListFetcher;
		var session;

		beforeEach(function() {
			fromListFetcher = App.FromListFetcher.create();
			session = App.getSession();
		});

		it("transfer is OwnAccountTransfer", function() {
			var transfer = App.OwnAccountTransfer.create();
			spyOn(fromListFetcher, 'processList');
			spyOn(bsoi, 'filterProductList');

			fromListFetcher.fetch(transfer, callback);

			expect(fromListFetcher.callbackTransfer).toEqual(callback);
            expect(bsoi.filterProductList).toHaveBeenCalled();

            var args = bsoi.filterProductList.mostRecentCall.args;
            var requestData = {
            	productFilter:{"functionCode" : "FUNC_FT_OWN", "crDrFlag" : "DR"},
            	requestHeader : session.getBreezeContext()
            };
            var response = jasmine.createSpy();

            expect(args[0]).toEqual(requestData);
            
            args[1](response);
            expect(fromListFetcher.processList).toHaveBeenCalledWith(transfer, response);
		});

		it("transfer is BillPayment", function() {
			var transfer = App.BillPayment.create();
			spyOn(fromListFetcher, 'processList');
			spyOn(bsoi, 'filterProductList');

			fromListFetcher.fetch(transfer, callback);

			expect(fromListFetcher.callbackTransfer).toEqual(callback);
            expect(bsoi.filterProductList).toHaveBeenCalled();

            var args = bsoi.filterProductList.mostRecentCall.args;
            var requestData = {
            	productFilter:{"functionCode" : "FUNC_BP", "crDrFlag" : "DR"},
            	requestHeader : session.getBreezeContext()
            };
            var response = jasmine.createSpy();

            expect(args[0]).toEqual(requestData);
            
            args[1](response);
            expect(fromListFetcher.processList).toHaveBeenCalledWith(transfer, response);
		});

		it("transfer is CreditCardPayment", function() {
			var transfer = App.CreditCardPayment.create();
			spyOn(fromListFetcher, 'processList');
			spyOn(bsoi, 'filterProductList');

			fromListFetcher.fetch(transfer, callback);

			expect(fromListFetcher.callbackTransfer).toEqual(callback);
            expect(bsoi.filterProductList).toHaveBeenCalled();

            var args = bsoi.filterProductList.mostRecentCall.args;
            var requestData = {
            	productFilter:{"functionCode" : "FUNC_FT_CARD_PAYMENT", "crDrFlag" : "DR"},
            	requestHeader : session.getBreezeContext()
            };
            var response = jasmine.createSpy();

            expect(args[0]).toEqual(requestData);
            
            args[1](response);
            expect(fromListFetcher.processList).toHaveBeenCalledWith(transfer, response);
		});

		it("transfer is ThirdPartyTransfer", function() {
			var transfer = App.ThirdPartyTransfer.create();
			spyOn(bsoi, 'filterProductList');

			fromListFetcher.fetch(transfer, callback);

			expect(fromListFetcher.callbackTransfer).toEqual(callback);
            expect(bsoi.filterProductList).toHaveBeenCalled();
		});
	});
	

});